from rest_framework import serializers
from blogapi.models import *


class IdField(serializers.Field):
    def to_representation(self, value):
        print('3', value)
        return '{:05d}'.format(value)


class TagsSerializer(serializers.ModelSerializer):

    class Meta:
        model = Tag
        fields = '__all__'


class CategoriesSerializer(serializers.ModelSerializer):

    class Meta:
        model = Category
        fields = '__all__'


class AuthorsSerializer(serializers.ModelSerializer):

    class Meta:
        model = Author
        fields = '__all__'


class CategoriesFiled(serializers.Field):
    def to_representation(self, value):
        return RArticalCategorySerializer(value, many=True).data


class TagsField(serializers.Field):
    def to_representation(self, value):
        return RArticalTagSerializer(value, many=True).data


class AuthorsField(serializers.Field):
    def to_representation(self, value):
        return RArticalAuthorsSerializer(value, many=True).data


class ArticalsSerializer(serializers.ModelSerializer):
    tags = TagsField(read_only=True, source='artical_tags')
    authors = AuthorsField(read_only=True, source='atrical_authors')
    categories = CategoriesFiled(read_only=True, source='artical_categories')

    class Meta:
        model = Artical
        fields = ('id', 'title', 'html_path', 'categories', 'tags', 'authors')


class RArticalTagSerializer(serializers.ModelSerializer):
    tagid = serializers.SlugRelatedField(read_only=True, slug_field='id')
    tag_name = serializers.SlugRelatedField(read_only=True, source='tagid', slug_field='tag_name')

    class Meta:
        model = RArticalTag
        fields = ('id', 'tagid', 'tag_name')


class RArticalTagCreateSerializer(serializers.ModelSerializer):

    class Meta:
        model = RArticalTag
        fields = ('id', 'articalid', 'tagid')


class RArticalAuthorsSerializer(serializers.ModelSerializer):
    authorid = serializers.SlugRelatedField(read_only=True, slug_field='id')
    first_name = serializers.SlugRelatedField(read_only=True, source='authorid', slug_field='first_name')
    mid_name = serializers.SlugRelatedField(read_only=True, source='authorid', slug_field='mid_name')
    last_name = serializers.SlugRelatedField(read_only=True, source='authorid', slug_field='last_name')
    des = serializers.SlugRelatedField(read_only=True, source='authorid', slug_field='des')

    class Meta:
        model = RArticalAuthor
        fields = ('id', 'authorid', 'first_name', 'mid_name', 'last_name', 'des')


class RArticalAuthorCreateSerializer(serializers.ModelSerializer):

    class Meta:
        model = RArticalAuthor
        fields = ('id', 'articalid', 'authorid')


class RArticalCategorySerializer(serializers.ModelSerializer):
    categoryid = serializers.SlugRelatedField(read_only=True, slug_field='id')
    cate_name = serializers.SlugRelatedField(read_only=True, source='categoryid', slug_field='cate_name')
    cate_desc = serializers.SlugRelatedField(read_only=True, source='categoryid', slug_field='cate_desc')

    class Meta:
        model = RArticalCategory
        fields = ('id', 'categoryid', 'cate_name', 'cate_desc')


class RArticalCategoryCreateSerializer(serializers.ModelSerializer):

    class Meta:
        model = RArticalCategory
        fields = ('id', 'articalid', 'categoryid')
